package com.hue.springbootquartz.config;

import com.hue.springbootquartz.job.MyJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob(MyJob.class). //传入要执行的任务类
                withIdentity("myjob").  //给任务设置id
                usingJobData("msg", "hello world!"). //每个JobDetail都包含了一个map，在自定义的Job类中可以通过JobExecutionContext来获取
                storeDurably(). //即使没有Trigger关联时，也不要删除这个JobDetail
                build();
    }

    @Bean
    public Trigger trigger() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");
        return TriggerBuilder.newTrigger().
                forJob(jobDetail()).    //关联jobDetail
                withIdentity("trigger").
                withSchedule(cronScheduleBuilder). //关联任务调度器
                build();
    }
}
